<template>
  <div class="app-container">
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="primary" @click="handleAdd()">
          <i-ep-plus />
          特性对比
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        :data="tableData"
        highlight-current-row
        border
        height="calc(100vh - 120px - 100px)"
      >
        <el-table-column
          label="工段"
          prop="workshopSectionCode"
          min-width="80"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="工序"
          prop="processCode"
          min-width="80"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="生产线"
          prop="equipCode"
          min-width="90"
          align="center"
          show-overflow-tooltip
        />
        <!-- <el-table-column
          label="项目描述"
          prop="projectName"
          min-width="90"
          align="center"
          show-overflow-tooltip
        /> -->
        <el-table-column
          label="型号描述"
          prop="modelName"
          min-width="90"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品型号"
          prop="modelCode"
          min-width="90"
          align="center"
          show-overflow-tooltip
        />
        <!-- <el-table-column
          label="检具设备"
          prop="gageEquipMarkName"
          min-width="90"
          align="center"
          show-overflow-tooltip
        /> -->
        <el-table-column
          label="检具类别"
          prop="groupCode"
          min-width="90"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="特性名称"
          prop="paramName"
          min-width="140"
          align="left"
          show-overflow-tooltip
        />
        <el-table-column
          label="公称尺寸"
          prop="parameterStandards"
          min-width="80"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="公差下限"
          prop="lowerLimitValue"
          min-width="80"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="公差上限"
          prop="upperLimitValue"
          min-width="80"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="预警工单"
          min-width="76"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag v-if="handleCheckAllChange1(scope.row)" type="success">
              是
            </el-tag>
            <el-tag v-else type="info"> 否</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="预警邮件"
          min-width="76"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag v-if="handleCheckAllChange2(scope.row)" type="success">
              是
            </el-tag>
            <el-tag v-else type="info"> 否</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="110" align="center">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialogdetail(scope.row)"
              >
                <i-ep-search />
              </el-button>
            </el-tooltip>

            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
              >
                <i-ep-edit />
              </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="取消对比"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="deleteItem(scope.$index)"
              >
                <i-ep-circleClose />
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 特性项选择 -->
    <pcmmeasurefeaturesContrast-select
      v-model:visible="selectDialog.visible"
      :data="breakList"
      @echo-info="changeVisible1"
    />

    <pcmmeasurefeatures-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => resetTableData(id)"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { mockList } from "./index";
import request from "@/utils/request";

defineOptions({
  name: "PCMPCMMEASUREFEATURESCONTRAST",
  inheritAttrs: false,
});

const tableData = ref([]);

onMounted(() => {});

/** 添加行 */
const handleAdd = () => {
  selectDialog.value.visible = true;
  if (tableData.value.length > 0) {
    getBreakList();
  }
};

// 新增完返回的数据用于显示选择器
const breakList = ref(<any>[]);
const getBreakList = () => {
  breakList.value = tableData.value;
  //   console.log("回显的数据", breakList.value);
};

const selectDialog = ref({
  visible: false,
});
const changeVisible1 = (val: any) => {
  if (val && val.length) {
    tableData.value = val;
  } else {
    tableData.value = [];
  }
};

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
// 查询详情
async function getInfo(id: string) {
  request({
    url: `/dmpDosPcm/pcmmeasurefeaturesgroup/get/${id}`,
    method: "get",
  })
    .then((data) => {
      data.data.isRatioUp = data.data.isRatioUp == 1;
      data.data.isRatioDow = data.data.isRatioDow == 1;
      data.data.isSingle = data.data.isSingle == 1;
      data.data.isAverage = data.data.isAverage == 1;
      data.data.isLong = data.data.isLong == 1;
      data.data.isStage = data.data.isStage == 1;
      data.data.isFeatures = data.data.isFeatures == 1;
      data.data.isSingleOrder = data.data.isSingleOrder == 1;
      data.data.isAverageOrder = data.data.isAverageOrder == 1;
      data.data.isLongOrder = data.data.isLongOrder == 1;
      data.data.isStageOrder = data.data.isStageOrder == 1;
      data.data.isFeaturesOrder = data.data.isFeaturesOrder == 1;
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开测量特性定义表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "产品特性配置对比项-修改";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "产品特性配置对比项-新增";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

async function synchronous() {}

/** 打开测量特性定义表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "产品特性配置对比项-详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

// 搜索项折叠
const collapsed = ref(false);

async function deleteItem(index: any) {
  ElMessageBox.confirm("确认取消已选中的特性对比数据?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    ElMessage.success("取消成功！");
    tableData.value.splice(index, 1);
  });
}

onMounted(() => {});

function resetTableData(id) {
  const currentIndex = tableData.value.findIndex((e) => e.id === id);
  console.log("currentIndex", currentIndex);
}

// 预警工单 回显
function handleCheckAllChange1(formData: any) {
  if (
    formData.isSingleOrder == 0 &&
    formData.isAverageOrder == 0 &&
    formData.isLongOrder == 0 &&
    formData.isStageOrder == 0 &&
    formData.isFeaturesOrder == 0
  ) {
    return false;
  } else {
    return true;
  }
}

// 预警邮件 回显
function handleCheckAllChange2(formData: any) {
  if (
    formData.isSingle == 0 &&
    formData.isAverage == 0 &&
    formData.isLong == 0 &&
    formData.isStage == 0 &&
    formData.isFeatures == 0
  ) {
    return false;
  } else {
    return true;
  }
}
</script>
